﻿#region << 版 本 注 释 >>

/*----------------------------------------------------------------
* 类 名 称 ：AssetBundleRes
* 类 描 述 ：
* 作    者 ：henrychang
* 创建时间 ：2019/1/6 18:38:08
*******************************************************************
* Copyright @ henrychang 2019. All rights reserved.
*******************************************************************
//----------------------------------------------------------------*/

#endregion << 版 本 注 释 >>

using System.Collections;
using TouchFish.ObjectPool;
using UnityEngine;
using System;

namespace TouchFish.AssetLoad
{
    public class AssetBundleRes : AbstractRes
    {
        private bool mUnloadFlag = true;
        private string[] mDependResList;
        private AssetBundleCreateRequest mAssetBundleCreateRequest;

        public static AssetBundleRes Allocate(string name)
        {
            var res = SafeObjectPool<AssetBundleRes>.Instance.Allocate();
            if (res != null)
            {
                res.AssetName = name;
                res.InitAssetBundleName();
            }

            return res;
        }

        private void InitAssetBundleName()
        {
            //mDependResList = ResDatas.Instance.GetAllDependenciesByUrl(AssetName);
        }

        public AssetBundle AssetBundle
        {
            get { return (AssetBundle)mAsset; }
            private set { mAsset = value; }
        }

        public override void AcceptLoaderStrategySync(IResLoader loader, IResLoaderStrategy strategy)
        {
            strategy.OnSyncLoadFinish(loader, this);
        }

        public override void AcceptLoaderStrategyAsync(IResLoader loader, IResLoaderStrategy strategy)
        {
            strategy.OnAsyncLoadFinish(loader, this);
        }

        public override bool LoadSync()
        {
            if (!CheckLoadAble())
            {
                return false;
            }

            State = ResState.Loading;

#if UNITY_EDITOR
            if (SimulateAssetBundleInEditor)
            {
            }
            else
#endif
            {
                //var url = ResKitUtil.AssetBundleName2Url(mAssetName);
                //var bundle = AssetBundle.LoadFromFile(url);

                mUnloadFlag = true;

                //if (bundle == null)
                //{
                //    //Log.E("Failed Load AssetBundle:" + mAssetName);
                //    OnResLoadFaild();
                //    return false;
                //}

                //AssetBundle = bundle;
            }

            State = ResState.Ready;

            return true;
        }

        public override void LoadAsync()
        {
            if (!CheckLoadAble())
            {
                return;
            }

            State = ResState.Loading;

            //ResMgr.Instance.PushIEnumeratorTask(this);
        }

        public override IEnumerator DoLoadAsync(Action finishCallback)
        {
            //开启的时候已经结束了
            if (RefCount <= 0)
            {
                OnResLoadFaild();
                finishCallback();
                yield break;
            }

#if UNITY_EDITOR
            if (SimulateAssetBundleInEditor)
            {
                yield return null;
            }
            else
#endif
            {
                //var url = ResKitUtil.AssetBundleName2Url(mAssetName);
                //var abcR = AssetBundle.LoadFromFileAsync(url);

                //mAssetBundleCreateRequest = abcR;
                //yield return abcR;
                //mAssetBundleCreateRequest = null;

                //if (!abcR.isDone)
                //{
                //    //Log.E("AssetBundleCreateRequest Not Done! Path:" + mAssetName);
                //    OnResLoadFaild();
                //    finishCallback();
                //    yield break;
                //}

                //AssetBundle = abcR.assetBundle;
            }

            State = ResState.Ready;
            finishCallback();
        }

        public override string[] GetDependResList()
        {
            return mDependResList;
        }

        public override bool UnloadImage(bool flag)
        {
            if (AssetBundle != null)
            {
                mUnloadFlag = flag;
            }

            return true;
        }

        public override void Recycle2Cache()
        {
            SafeObjectPool<AssetBundleRes>.Instance.Recycle(this);
        }

        public override void OnRecycled()
        {
            base.OnRecycled();
            mUnloadFlag = true;
            mDependResList = null;
        }

        protected override float CalculateProgress()
        {
            if (mAssetBundleCreateRequest == null)
            {
                return 0;
            }

            return mAssetBundleCreateRequest.progress;
        }

        protected override void OnReleaseRes()
        {
            if (AssetBundle != null)
            {
                AssetBundle.Unload(mUnloadFlag);
                AssetBundle = null;
            }
        }
    }
}